﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Http;
using System.IO;
using System.ServiceModel.Syndication;

namespace HttpClient
{
    class Program
    {
        static void Main(string[] args)
        {
            Uri baseUri = new Uri("http://127.0.0.1:81/PictureRestService/Service.svc/pictures");

            AtomPubClient client = new AtomPubClient();
            client.BaseAddress = baseUri;

            Console.WriteLine("TESTING ATOMPUB ENTRY METHODS");
            
            // POST entry
            SyndicationItem item = new SyndicationItem();
            item.Authors.Add(new SyndicationPerson("bob@contoso.com"));
            item.Title = new TextSyndicationContent("My first post");
            item.Content = new TextSyndicationContent("This is my first post on my blog.");

            SyndicationItem entryResource = client.AddEntry(baseUri, item);
            Console.WriteLine("Service created the following entry:");
            Console.WriteLine(entryResource.GetEditLink(true).Uri);
            Console.ReadLine();

            // GET entry can be tested implicitly by the user via their browser

            // PUT entry
            item.Title = new TextSyndicationContent("My second post");
            item.Content = new TextSyndicationContent("This is my second post.");
            client.UpdateEntry(entryResource.GetEditLink(true).Uri, item);

            Console.WriteLine("Updated entry:");
            Console.WriteLine(entryResource.GetEditLink(true).Uri);
            Console.ReadLine();

            // DELETE entry
            client.DeleteEntry(entryResource.GetEditLink(true).Uri);
            Console.WriteLine("Deleted entry at:");
            Console.WriteLine(entryResource.GetEditLink(true).Uri);
            Console.ReadLine();

            Console.WriteLine("TESTING ATOMPUB MEDIA ENTRY METHODS");

            // POST media
            HttpContent squareMedia;
            SyndicationItem mediaResource;
            using(FileStream stream = new FileStream("square.png", FileMode.Open))
            {
                squareMedia = HttpContent.Create(stream);
                mediaResource = client.AddMediaResource(baseUri, "image/png", "Test picture", squareMedia);
            }

            Console.WriteLine("Created media (picture of a square):");
            Console.WriteLine(mediaResource.GetEditMediaLink(true).Uri);
            Console.WriteLine("Service created the following metadata entry:");
            Console.WriteLine(mediaResource.GetEditLink(true).Uri);
            Console.ReadLine();

            // GET media can be tested implicitly by the user via their browser

            // PUT media
            HttpContent circleMedia;
            using (FileStream stream = new FileStream("circle.png", FileMode.Open))
            {
                circleMedia = HttpContent.Create(stream);
                client.Put(mediaResource.GetEditMediaLink(true).Uri, "image/png", circleMedia);
            }

            Console.WriteLine("Updated media (picture of a circle):");
            Console.WriteLine(mediaResource.GetEditMediaLink(true).Uri);
            Console.ReadLine();

            // DELETE media
            client.Delete(mediaResource.GetEditMediaLink(true).Uri);
            Console.WriteLine("Deleted media at:");
            Console.WriteLine(mediaResource.GetEditMediaLink(true).Uri);
            Console.ReadLine();


        }
    }
}
